home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / ms_sh22b.zip / src / swap.asm < prev    next >
Assembly Source File  |  1993-12-01  |  23KB  |  1,356 lines

  1.     TITLE   swap.asm
  2.     NAME    swap
  3.     .8087
  4.  
  5. ; MS-DOS System Function with Swaping - Front End
  6. ;
  7. ; MS-DOS System - Copyright (c) 1990,1,2 Data Logic Limited
  8. ;
  9. ; This code is subject to the following copyright restrictions:
  10. ;
  11. ; 1.  Redistribution and use in source and binary forms are permitted
  12. ;     provided that the above copyright notice is duplicated in the
  13. ;     source form.
  14. ;
  15. ;    $Header: /usr/users/istewart/src/shell/sh2.2/RCS/swap.asm,v 2.0 1992/05/21 16:49:54 Ian_Stewartson Exp $
  16. ;
  17. ;    $Log: swap.asm,v $
  18. ;    Revision 2.0  1992/05/21  16:49:54  Ian_Stewartson
  19. ;    MS-Shell 2.0 Baseline release
  20. ;
  21. ;    Revision 2.0  1992/04/13  17:39:45  Ian_Stewartson
  22. ;    MS-Shell 2.0 Baseline release
  23. ;
  24. ;
  25. ; MODULE DEFINITION:
  26. ;
  27. ; This is the front end for the swapping version of system.  When linked
  28. ; in, it should be the first module in the load line so that it appears in
  29. ; memory immediately after the psp.  For example:
  30. ;
  31. ;   link swap+x1+x2+x3+system;
  32. ;
  33. ; This module has no user changeable features.  All selections are
  34. ; performed in the associated system function.
  35. ;
  36. ; If you want small model, you can only get small model data.  This
  37. ; requires this module to be compiled with the -DSMALL_DATA switch to the
  38. ; assembler.
  39. ;
  40. ; More details in system.c
  41. ;
  42. ; Author:
  43. ;    Ian Stewartson
  44. ;    Data Logic, Queens House, Greenhill Way
  45. ;    Harrow, Middlesex  HA1 1YR, UK.
  46. ;    istewart@datlog.co.uk or ukc!datlog!istewart
  47. ;
  48. ;
  49.  
  50. ;
  51. ; Segment declarations
  52. ;
  53.  
  54. SWAP_TEXT    segment word public 'CODE'
  55. SWAP_TEXT    ends
  56.  
  57. _DATA        segment word public 'DATA'
  58. _DATA        ends
  59.  
  60. CONST        segment word public 'CONST'
  61. CONST        ends
  62.  
  63. _BSS        segment word public 'BSS'
  64. _BSS        ends
  65.  
  66. DGROUP        group    CONST, _BSS, _DATA
  67.  
  68. ;
  69. ; Declare external functions and data
  70. ;
  71.     extrn    __maperror:far
  72.     extrn    _errno:word
  73.     extrn    __psp:word
  74.  
  75. ;
  76. ; Start of the spawn function
  77. ;
  78.  
  79. SWAP_TEXT    segment
  80.         assume  cs: SWAP_TEXT, ds: NOTHING, ss: DGROUP
  81.  
  82. ;
  83. ; For this function, all the code and data space are in the code space
  84. ;
  85.         public    _cmd_line
  86.         public    _path_line
  87.         public    _SW_intr
  88.         public    _SW_Blocks
  89.         public    _SW_fp
  90.         public    _SW_EMstart
  91.         public    _SW_Mode
  92.         public    _SW_EMSFrame
  93.         public    _SW_Int00
  94.         public    _SW_Int23
  95.         public    _SW_XMS_Driver
  96.         public    _SW_XMS_Gversion
  97.         public    _SW_XMS_Allocate
  98.         public    _SW_XMS_Free
  99.         public    _SW_XMS_Available
  100.         public    _SW_I23_InShell
  101.  
  102.  
  103. _cmd_line    db    129 dup (?)    ; Command line
  104. _path_line    db    80 dup (?)    ; Path line
  105. _SW_Blocks    dw    0        ; Number of blocks to read/write
  106. _SW_fp        dw    0ffffH        ; File ID
  107. _SW_EMstart    dd    0100000H    ; Default Extended Mem start
  108. _SW_Mode    dw    0        ; Type of swapping to do
  109.                     ;   1 - disk
  110.                     ;   2 - Extended memory
  111.                     ;   3 - EMS driver
  112.                     ;   4 - XMS driver
  113. _SW_EMSFrame    dw    0        ; EMS Frame segment
  114. _SW_intr    dw    0        ; Interrupt 23 detected.
  115. _SW_XMS_Driver    dd    0        ; XMS Driver Interface
  116. _SW_I23_InShell    db    0        ; In shell flag for Interrupt 23
  117.  
  118.  
  119. ;
  120. ; Some addition variables
  121. ;
  122.  
  123. SW_LMstart    dd    0        ; Low Mem start for Extended Mem swap
  124. N_mcb        dw    0        ; Start write address
  125. Result        dw    0        ; Return value
  126.  
  127. ;
  128. ; Stack save pointers
  129. ;
  130.  
  131. S_ss        dw    0            ; Save Stack pointers
  132. S_sp        dw    0
  133. S_di        dw    0            ; Save DI, SI
  134. S_si        dw    0
  135. S_ds        dw    0            ; Save the original DS
  136.  
  137. ;
  138. ; Two blank FCB
  139. ;
  140.  
  141. FCB1        dw    16    dup (?)
  142. FCB2        dw    16    dup (?)
  143.  
  144. ;
  145. ; XMS Driver Move structure
  146. ;
  147.  
  148. XMS_DIF        equ    $
  149. XMS_Length    dd    0            ; Number of bytes
  150. XMS_SHandle    dw    0            ; Source Handler
  151. XMS_Soffset    dd    0            ; Source Offset
  152. XMS_DHandle    dw    0            ; Destination Handler
  153. XMS_Doffset    dd    0            ; Destination Offset
  154.  
  155. ;
  156. ; Extended Memory Global Descriptor tables
  157. ;
  158.         org    XMS_DIF
  159. GD_table    equ    $
  160. GDT_Dummy    dw    4    dup (0)        ; Dummy
  161. GDT_self    dw    4    dup (0)        ; For self
  162. GDT_src        equ    $            ; Source
  163.         dw    04000H            ; Length - 16K bytes
  164. GDT_src_low    dw    0            ;     Low Order address
  165. GDT_src_high    db    0            ;     High Order address
  166.         db    093h            ;     Access Rights
  167.         dw    0            ;     Reserved
  168. GDT_dest    equ    $            ; Destination
  169.         dw    04000H            ;     Length - 16K bytes
  170. GDT_dest_low    dw    0            ;     Low Order address
  171. GDT_dest_high    db    0            ;     High Order address
  172.         db    093h            ;     Access Rights
  173.         dw    0            ;     Reserved
  174. GDT_bios    dw    4    dup (0)        ; Bios
  175. GDT_stack    dw    4    dup (0)        ; Stack
  176.  
  177. ;
  178. ; Execute interrupt structure
  179. ;
  180.  
  181. exec_parms    equ    $
  182. exec_env    dw    0
  183.         dw    offset _cmd_line    ; Command line address
  184. exec_cseg    dw    ?
  185.         dw    offset FCB1        ; FCB1 address
  186. exec_f1seg    dw    ?
  187.         dw    offset FCB2        ; FCB1 address
  188. exec_f2seg    dw    ?
  189.  
  190. Swap_PANIC    db    'PANIC: Swap file re-load error - REBOOT', 0aH, 0dH
  191.         db    '$'
  192.  
  193. Swap_DZERO    db    'PANIC: Divide by zero', 0aH, 0dH
  194.         db    '$'
  195.  
  196. ;
  197. ; OK - exec requires a local stack, cause some programs overwrite it
  198. ;
  199.         even
  200.         db    398 dup (0)
  201. Local_Stack:
  202.         dw    0
  203.  
  204. ;
  205. ; Code starts
  206. ;
  207.     public    _SA_spawn
  208.  
  209. SA_spawn1    proc    far
  210.     mov    ds, word ptr cs:exec_env    ; Load Env seg.
  211.     xor    si, si                ; Clear start offset
  212.  
  213. ;
  214. ; Copy into Env Seg
  215. ;
  216.  
  217. $Copy_Env:
  218. ifndef SMALL_DATA
  219.     les    bx, dword ptr ss:[bp + 6]    ; Check for end of loop
  220.     mov    ax, word ptr es:[bx + 0]
  221.     or    ax, word ptr es:[bx + 2]
  222. else
  223.     mov    dx, word ptr cs:S_ds        ; Get original DS
  224.     mov    es, dx                ; and dump it in dx for later
  225.     mov    bx, word ptr ss:[bp + 6]    ; Check for end of loop
  226.     mov    ax, word ptr es:[bx + 0]
  227.     or    ax, ax
  228. endif
  229.     je    $Copy_End
  230.  
  231. ;
  232. ; Save start address
  233. ;
  234. ; For small data model,the DX register contains the original DS
  235. ;
  236.  
  237. ifndef SMALL_DATA
  238.     add    word ptr ss:[bp + 6], 4        ; Increment environment by 4
  239.  
  240.     mov    cx, word ptr es:[bx + 0]    ; Load address of cur Env string
  241.     mov    ax, word ptr es:[bx + 2]    ; into es:bx
  242. else
  243.     add    word ptr ss:[bp + 6], 2        ; Increment environment by 2
  244.  
  245.     mov    cx, word ptr es:[bx + 0]    ; Load address of cur Env string
  246.     mov    ax, dx                ; into es:bx
  247. endif
  248.     mov    es, ax
  249.     mov    bx, cx
  250.  
  251. ;
  252. ; Copy this value
  253. ;
  254.  
  255. $Copy_Val:
  256.     mov    al, byte ptr es:[bx]    ; Copy across
  257.     mov    byte ptr ds:[si], al
  258.     inc    bx            ; Increment pointers
  259.     inc    si
  260.     or    al, al
  261.     jne    $Copy_Val
  262.     jmp    $Copy_Env
  263.  
  264. ;
  265. ; Set up exec parameter block     - DS is on stack
  266. ;
  267. $Copy_End:
  268.     xor    ax, ax
  269.     mov    word ptr ds:[si], ax    ; Terminate environment
  270.     add    si, 2
  271.  
  272. ;
  273. ; Set up new program length
  274. ;
  275.     add    si, 16            ; Round up paras
  276.     mov    dx, si            ; Save end offset in DX
  277.     mov    bx, ds
  278.  
  279.     mov    cl, 4
  280.     shr    si, cl            ; # paras used by Env
  281.     add    si, bx            ; End para number
  282.  
  283.     mov    bx, word ptr cs:N_mcb    ; Load our MCB address in BX
  284.     mov    ax, bx
  285.     inc    ax
  286.     sub    si, ax
  287.     mov    cx, si            ; Save new max paras in CX
  288.  
  289. ;
  290. ; Use interrupt 4a to shrink memory.  First release all memory above us.
  291. ;
  292.     push    ax
  293.     push    cx            ; Save Max paras and location
  294.     mov    ds, bx            ; Set up the segement for MCB
  295.     mov    cx, word ptr ds:3    ; Get the MCB length
  296.  
  297. ; Are we the only one in the chain?
  298.  
  299.     cmp    byte ptr ds:0, 'Z'    ; End of chain ?
  300.     jz    $Shrink_First
  301.  
  302. ;
  303. ; Loop round releasing memory blocks
  304. ;
  305. ;    CX - original length of block;
  306. ;    DS - segement of the previous block
  307. ;
  308. $Shrink_Next:
  309.     mov    ax, ds            ; Move to the next block
  310.     add    cx, ax
  311.     inc    cx
  312.     mov    ds, cx
  313.  
  314.     cmp    byte ptr ds:0, 'Z'    ; End of chain ?
  315.     jz    $Shrink_First
  316.  
  317.     mov    cx, word ptr ds:3    ; Save the length of this block
  318.  
  319.     mov    ax, ds            ; Advance to the block itself
  320.     inc    ax
  321.     mov    es, ax            ; Set up Block address
  322.  
  323.     mov    ah, 049H
  324.     int    021H
  325.     jmp    $Shrink_Next
  326.  
  327. ;
  328. ;    Shrink the PSP segment
  329. ;
  330.  
  331. $Shrink_First:
  332.     pop    cx
  333.     pop    ax
  334.         mov    es, ax            ; Set PSP address
  335.     mov    bx, cx            ; Set max length
  336.     mov    ah, 04aH
  337.     int    021H
  338.  
  339. ;
  340. ; Execute function
  341. ;
  342.  
  343.     mov    word ptr cs: S_sp, sp    ; Save the current stack
  344.     mov    word ptr cs: S_ss, ss
  345.  
  346. ;
  347. ; Move to the local stack so that it doesn't get overwritten.
  348. ;
  349.     mov    ax, cs
  350.     cli
  351.     mov    sp, offset Local_Stack
  352.     mov    ss, ax
  353.     sti
  354.  
  355. ; Clear out Interrupts
  356.  
  357.     mov    ah, 00bH        ; Check Keyboard status
  358.     int    021H
  359.  
  360. ;
  361. ;  Check for interrupt 23 detected
  362. ;
  363.     mov    ax, word ptr cs:_SW_intr
  364.     or    ax, ax
  365.     jz    $I23_Cf            ; No - continue;
  366.  
  367. ;
  368. ; Interrupt 23 detected - abort
  369. ;
  370.     mov    ax, cs            ; Set up for reload
  371.     cli
  372.     mov    sp, offset Local_Stack
  373.     mov    ss, ax
  374.     sti
  375.  
  376.     mov    ds, word ptr cs:S_ds    ; Restore DS
  377.     xor    ax, ax
  378.     jmp    $Exec_Complete
  379.  
  380. ;
  381. ; No interrupts - continue
  382. ;
  383. $I23_Cf:
  384.     mov    ax, cs            ; Set up segments
  385.     mov    es, ax
  386.     mov    ds, ax
  387.  
  388.     mov    ax, 04b00H        ; Load and execute function
  389.     mov    dx, offset _path_line    ; Load path
  390.     mov    bx, offset exec_parms    ; Load the execute structure
  391.     mov    byte ptr cs:_SW_I23_InShell, 1    ; Set not shell flag
  392.     int    021H
  393.     mov    byte ptr cs:_SW_I23_InShell, 0    ; Set in shell flag
  394.  
  395. ; Disable interrupts while we restore the stack to the local one
  396.  
  397.     mov    ax, cs
  398.     cli
  399.     mov    sp, offset Local_Stack
  400.     mov    ss, ax
  401.     sti
  402.  
  403. ;
  404. ; Did an error occur?
  405. ;
  406.     jnc    $Exec_OK
  407.  
  408. ;